package com.oig.sys.auth.dao;

import com.oig.sys.auth.entity.TbResourcePermission;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

public interface ITbResourcePermissionDao extends JpaRepository<TbResourcePermission, Integer> {


    TbResourcePermission getByResourceIdAndPermissionId(Integer resourceId, Integer permissionId);

    @Query("select rp.id from TbResourcePermission rp where rp.resourceId=:resourceId")
    List<Integer> findAllIdByResourceId(@Param("resourceId") Integer resourceId);

    @Query("select rp.id from TbResourcePermission rp where rp.resourceId=:resourceId and rp.id not in(:idList)")
    List<Integer> findResourcePermissionExIds(@Param("resourceId") Integer resourceId, @Param("idList") List<Integer> idList);

    @Modifying
    @Query("delete from TbResourcePermission rp where rp.id in(:idList)")
    void deleteByIdList(@Param("idList") List<Integer> idList);

    @Query("select distinct rp.permissionId from TbResourcePermission rp where rp.resourceId in(:resourceIdList)")
    List<Integer> findPermissionIdByResourceIdList(@Param("resourceIdList") List<Integer> resourceIdList);
}
